<template>
  <div class="collection">

    <div class="type">
      <div class="type-head">
        <el-image class="type-text" :src="require('@/assets/logo/logoH.png')" @click="pushHome()"/>
      </div>

      <div class="cartButton" :style="moreCart ? {borderBottom:'none', backgroundColor: 'white'} : {}" v-on:mouseenter="getCartItem()" v-on:mouseleave="moreCart = false">
        <span class="pushCart">我的购物车</span>
        <el-badge :value="cartList.length" class="item" style="font-size: 20px;height: 20px;line-height: 20px;">
          <i class="el-icon-shopping-cart-1" style="margin-left: 10px"></i>
        </el-badge>
      </div>
      <div id="moreCart" class="moreCart" v-show="moreCart" v-on:mouseover="moreCart = true" v-on:mouseout="moreCart = false">
        <div class="moreCartProduct">
          <div style="text-align: left;padding-left: 10px;margin-top: 10px;margin-bottom: 10px;font-size: 13px;font-weight: bold">最新加入的商品</div>
          <div class="moreCartDetail" v-for="item in cartList" :key="item.skuId">
            <el-image
              class="moreCartDetailImg"
              :src="item.skuImg"
              @click="pushProductDetail(item.skuId)"
              fit="cover"></el-image>
            <div class="moreCartDetailTitle" @click="pushProductDetail(item.skuId)">{{ item.skuTitle }}</div>
            <div style="float: right;font-size: 12px;text-align: right">
              <div><span style="color: red">￥{{ item.skuPrice }}.00</span>×{{ item.count }}</div>
              <div class="moreCartDetailDel" @click="delCartItem(item.skuId)">删除</div>
            </div>
          </div>
        </div>
        <div class="moreCartBottom">
          <span style="font-size: 12px">共{{ cartList.length }}件商品</span>
          <div class="morePushCart" @click="$router.push({path: '/cart'})">去购物车</div>
        </div>
      </div>

      <form class="find-button">
        <input class="find-input" type="text" placeholder="请输入您要搜索的内容..." v-model="searchText">
        <button class="find-click" type="submit" @click="searchButton()"><i class="el-icon-search"/></button>
      </form>
    </div>

    <el-row class="collection_menu">
      <el-col :span="5">
        <div class="collection_menu_left">
          <div class="collection_menu_left_input" v-for="list in category.slice(0, 10)" :key="list.catId" v-on:mouseover="mouseOver(list.catId)" v-on:mouseout="mouseLeave()">
            {{ list.name }}
            <i class="el-icon-caret-right" style="float: right; margin-right: 20px; font-size: 18px;font-weight: normal; line-height: 50px"/>
          </div>
        </div>
      </el-col>
      <el-col :span="19">
        <div class="collection_menu_right" v-show="menu_right" @mouseover="mouseOver" @mouseout="mouseLeave">
          <div v-for="list in categoryTreeById.slice(0, 10)" :key="list.catId" style="height: 50px;line-height: 50px;padding-left: 20px">
            <div style="width: 120px; display: inline-block; text-align: right;font-weight: bold">
              {{ list.name.length > 5 ? list.name.substr(0, 5) + '...' : list.name }}
              <i class="el-icon-arrow-right" style="float: right; margin-right: 20px; font-size: 18px;font-weight: normal; line-height: 50px"/>
            </div>
            <div v-for="item in list.children.slice(0, 6)" :key="item.catId" style=" margin-left: 10px; display: inline-block">
              <span style="margin-right: 10px;color: rgb(176,176,176);">|</span>
              <span class="categoryButton" @click="pushCategoryId(item.catId, item.name)" style="width: 95px;display: inline-block;text-align: center">
                {{ item.name.length > 5 ? item.name.substr(0, 5) + '...' : item.name }}
              </span>
            </div>
            <span style="color: rgb(176,176,176);margin-left: 10px">|</span>
            <span v-if="list.children.length > 6" class="categoryButton" @click="pushCategoryId()" style="float: right;margin-right: 20px">
              查看更多
              <i class="el-icon-video-play"></i>
            </span>
          </div>
        </div>
        <div class="block" v-show="!menu_right">
          <el-carousel trigger="click" height="500px">
            <el-carousel-item v-for="item in homeLBT" :key="item.id">
              <el-image
                style="width: 1030px;height: 500px"
                :src="item.url"
                fit="fill">
              </el-image>
            </el-carousel-item>
          </el-carousel>
        </div>
      </el-col>
    </el-row>

    <div class="seckill" v-show="seckillSku !== null && seckillExist">
      <div class="seckill-left" :style="{backgroundImage:`url(${seckill_left})`}">
        <div class="seckill-left-text">秒杀</div>
        <div class="seckill-left-text1"><span style="font-weight: bold;font-size: 18px">
          {{ seckillSku !== null ? new Date(seckillSku[0].startTime).getHours() : '00' }}:
          {{ seckillSku !== null ? new Date(seckillSku[0].startTime).getMinutes() < 10 ? "0" + new Date(seckillSku[0].startTime).getMinutes() : new Date(seckillSku[0].startTime).getMinutes() : '00' }}
        </span> 点场 距结束</div>
        <div class="seckill-left-text2">
          <div class="seckill-left-text2-one">{{ countdownHour }}</div>
          :
          <div class="seckill-left-text2-one">{{ countdownMinute }}</div>
          :
          <div class="seckill-left-text2-one">{{ countdownSecond }}</div>
        </div>
      </div>
      <div class="seckill-right">
        <div class="seckill-right-item" v-for="list in seckillSku" :key="list.id" :title="list.skuInfoTO.skuName">
          <el-image class="seckill-right-item-image" :src="list.skuInfoTO.skuDefaultImg"/>
          <div class="seckill-right-item-text">{{ list.skuInfoTO.skuName }}</div>
          <div class="seckill-right-item-pay">￥{{ Number(list.skuInfoTO.price) - Number(list.seckillPrice) }}</div>
          <div></div>
        </div>
      </div>
    </div>

    <div v-for="item in list" :key="item.catalogId" class="tag_menu">
      <div class="tag">
        <span> {{ item.catalogName }} </span>
        <span class="span-more" @click="pushCategoryId(item.catalogId, item.catalogName)">查看更多<i style ="margin-left: 5px" class="el-icon-caret-right"/></span>
      </div>
      <div class="store">
        <div class="store-div inline-div">
          <el-card shadow="hover" v-for="list in item.products" :key="list.id" class="store-list inline-div">
            <div @click="pushProductDetail(list.skuId)">
              <el-image class="store-list-image" :src="list.skuImg"/>
<!--              <span class="title-span">{{ list.skuTitle.length > 19 ? list.skuTitle.substring(0, 19) : list.skuTitle }}</span><br>-->
              <span class="title-span">{{ list.skuTitle }}</span><br>
              <span class="title">
                <div class="title-div">
  <!--                <img class="title-money" src="../../assets/common/money2.png"/>-->
                  <span style="margin-left: 2px;"><span style="font-size: 14px;">￥</span>{{ list.skuPrice }}<span style="font-size: 14px;">.00</span></span>
                </div>
              </span>
            </div>
          </el-card>
        </div>
      </div>
    </div>

  </div>
</template>

<script>

import { Loading } from 'element-ui';

export default {
  name: 'Collection',
  data() {
    return {
      moreCart: false,
      searchText: '',
      menu_right: false,
      homeLBT: [
        {
          id: 1,
          url: require('@/assets/homeLBT/a1.png')
        },
        {
          id: 2,
          url: require('@/assets/homeLBT/b1.png')
        },
        {
          id: 3,
          url: require('@/assets/homeLBT/c1.jpg')
        },
        {
          id: 4,
          url: require('@/assets/homeLBT/d1.png')
        }
      ],
      list: [],
      listModel: {},
      category: [],
      categoryId: [],
      categoryTree: [],
      categoryTreeById: [],
      cartList: [],
      seckillExist: false,
      seckill_left: require('@/assets/seckill/seckill.png'),
      // now是实时的时间，也就是流动的时间
      now: NaN,
      // time是设定的倒计时，如3分钟，换算成和timestamp一样的单位，就是3 * 60000 = 180000
      // 需要自己定义倒计时时间
      time:0,
      seckillSku:[{
        startTime: null,
        skuInfoTO: {
          skuName: null
        }
      }]
    }
  },
  created() {
    this.getSeckillInfo()
    this.getLevelOneCategory()
    this.getCartItem()
    this.moreCart = false
  },
  methods: {
    getSeckillInfo(){
      this.$store.dispatch('seckill/seckill/currentSeckillSkuList')
        .then((data)=>{
          this.seckillSku = data.data
          console.log(this.seckillSku.length)
          if(this.seckillSku.length > 0){
            this.seckillExist = true
            this.time = this.seckillSku[0].endTime - new Date().getTime()
            const update = () => {
              // 也是循环调用自身,这次会在一开始就更新this.now
              this.now = Date.now();
              // requestAnimationFrame接受一个参数，这个参数是一个函数
              // 因为在update里调用requestAnimationFrame(update)，相当于设定下次屏幕刷新前执行一下update
              // 那么下次执行update的时候，又会设置下一帧要调用update
              //更新的事件由API把握
              //requestAnimationFrame在下次屏幕刷新前,所以不同的显示器，刷新率不一样，调用的次数也不同,但是，能保证，每次重绘的时候总是最新的时间
              requestAnimationFrame(update);
            };
            update();
            console.log(this.seckillSku)
          }
        })
        .catch(()=>{

        })
    },
    pushHome(){
      this.$router.push('/collection')
    },
    getCartItem(){
      this.moreCart = true
      // let loadingService = Loading.service({
      //   target: '#moreCart',
      //   lock: true
      // });
      this.$store.dispatch('cart/cart/getCartItem')
        .then((data)=>{
          this.cartList = data.data
          // loadingService.close()
        })
        .catch(()=>{})
    },
    searchButton(){
      if(this.searchText !== ''){
        let routeData = this.$router.resolve({
          path: "/search",
          query: {
            keyword: this.searchText
          }
        });

        //必要操作，否则不会打开新页面
        window.open(routeData.href, '_blank');
      }
    },
    mouseOver(id){
      this.menu_right = true
      this.categoryId = id
      this.categoryTree.forEach(item => {
        if(item.catId === id){
          this.categoryTreeById = item.children
        }
      })
    },
    mouseLeave(){
      this.menu_right = false
    },
    getLevelOneCategory(){
      this.$store.dispatch('goods/category/getCategoryByOneLevel')
        .then((data) => {
          this.category = data.data
        })
        .catch(()=>{})

      this.$store.dispatch('goods/category/getCategoryByTreeWithTen')
        .then((data) => {
          this.categoryTree = data.data
        })
        .catch(()=>{})

      this.$store.dispatch('goods/reception/getSkuInfoHome')
        .then((data) => {
          this.list = data.data
        })
        .catch(()=>{})
    },
    pushCategoryId(id ,name){
      let routeData = this.$router.resolve({
        path: "/search",
        query: {
          catId:id,
          name:name
        }
      });

      //必要操作，否则不会打开新页面
      window.open(routeData.href, '_blank');
    },
    pushProductDetail(id){
      let routeData = this.$router.resolve({
        path: "/product-detail",
        query: {
          skuId: id
        }
      });

      //必要操作，否则不会打开新页面
      window.open(routeData.href, '_blank');
    },
    delCartItem(skuId){
      this.$store.dispatch('cart/cart/delCartItem', skuId)
        .then(()=>{
          this.getCartItem()
        })
        .catch(()=>{})
    }
  },
  computed: {
    // 结束时间
    finishTime() {
      //这里并没有用this.now,因为this.now记录的是实时时间
      //这里的是设定的时间+设定的倒计时毫秒数
      return Date.now() + this.time;
      //this.time的变化，触发这个结束的时间，只要this.time不更新，结束时间就不会重新计算，所以结束时间一直固定
    },
    //剩余的毫秒数
    countdown() {
      //倒计时不会小于0，也就是说countdown最小值是0
      return Math.max(0, this.finishTime - this.now);
    },
    // 时
    countdownHour() {
      //padStart是用于补位
      return String.prototype.padStart.call(
        (this.countdown / 3.6e6) | 0,
        2,
        "0"
      );
    },
    // 分
    countdownMinute() {
      return String.prototype.padStart.call(
        ((this.countdown % 3.6e6) / 6e4) | 0,
        2,
        "0"
      );
    },
    // 秒
    countdownSecond() {
      return String.prototype.padStart.call(
        ((this.countdown % 6e4) / 1e3) | 0,
        2,
        "0"
      );
    }
  },
  // 监听时间到了：
  watch: {
    countdown(countdown) {
      if (0 === countdown) {
        this.$message({
          message: '当前秒杀场次已过请刷新页面',
          type: 'success'
        })
      }
    }
  }
}
</script>

<style lang="scss">
.el-card__body {
  padding: 0px;
}
.el-badge__content{
  background-color: rgb(60,133,39);
}
</style>

<style lang="scss" scoped>

.categoryButton {
  color: rgb(176,176,176);

  &:hover {
    cursor: pointer;
    color: rgb(90,181,35);
  }
}

.find-button {
  background-color: white;
  position: relative;
  width: 400px;
  float: right;
  //margin-right: 400px;
  border: 1px solid rgb(178,178,178);
  margin-top: 19px;

  .find-input {
    background: transparent;
    width: 100%;
    height: 42px;
    padding-left: 13px;
    padding-right:46px;
    border: none;
    outline: none;
    font-size: 10px;
  }

  .find-click {
    background: transparent;
    top: 0;
    right: 0;
    height: 42px;
    width: 42px;
    cursor: pointer;
    position: absolute;
    border: none;
    outline: none;
  }
}

.inline-div {
  display: inline-block;
}
.collection {
  height: 100%;
  width: 100%;
  background-color: rgb(245,245,245);

  .collection_menu{
    width: 1300px;
    height: 500px;
    margin: 0 auto;

    .collection_menu_left{
      background-color: rgb(81,79,78);
      height: 500px;

      .collection_menu_left_input{
        height: 50px;
        line-height: 50px;
        padding-left: 30px;
        color: white;
        font-weight: bold;
        font-size: 14px;

        &:hover{
          cursor: pointer;
          background-color: rgb(79,159,52);
        }
      }
    }

    .collection_menu_right{
      width: 100%;
      height: 500px;
      background-color: rgb(255, 255, 255);
    }
  }

  .type {
    width: 1300px;
    height: 80px;
    margin: 0 auto;
    text-align: center;
    //padding-top: 20px;
    //padding-bottom: 20px;

    .type-head {
      width: 130px;
      height: 80px;
      line-height: 80px;
      display: inline-block;
      float: left;

      .type-text {
        height: 50px;
        width: 130px;
        margin-top: 15px;
        //display: inline-block;
        //font-size: 16px;
        //margin-top: 13px;
        //margin-right: 20px;
        //float: left;

        &:hover {
          cursor: pointer;
          //color: rgb(79,159,52);
        }
      }
    }

    .cartButton{
      width: 150px;
      height: 42px;
      line-height: 42px;
      display: inline-block;
      float: right;
      margin-top: 19px;
      font-size: 13px;
      margin-left: 40px;
      border: 1px solid rgb(200, 200, 200);
      background-color: rgb(230, 230, 230);
      color: rgb(60,133,39);
      z-index: 51;
      position: relative;

      .pushCart{
        &:hover{
          cursor: pointer;
        }
      }
    }
    .moreCart{
      position: absolute;
      top: 60px;
      right: 301px;
      width: 350px;
      background-color: white;
      z-index: 50;
      border: 1px solid rgb(200,200,200);

      .moreCartProduct{


        .moreCartDetail{
          height: 70px;
          padding: 10px;

          .moreCartDetailImg{
            width: 50px;
            height: 50px;
            float: left;

            &:hover{
              cursor: pointer;
            }
          }

          &:hover{
            background-color: rgb(230,230,230);
          }
        }

        .moreCartDetailDel{
          margin-top: 5px;

          &:hover{
            cursor: pointer;
            color: rgb(60,133,39);
          }
        }

        .moreCartDetailTitle{
          display: inline-block;
          float: left;
          color: rgb(102,102,102);
          margin-left: 10px;
          text-align: left;
          width: 100px;
          height: 50px;
          line-height: 17.5px;
          font-size: 12px;
          text-overflow: ellipsis;
          -o-text-overflow: ellipsis;
          overflow: hidden;

          &:hover{
            cursor: pointer;
            color: rgb(60,133,39);
          }
        }
      }

      .moreCartBottom{
        height: 50px;
        background-color: rgb(230,230,230);

        span{
          display: inline-block;
          float: left;
          line-height: 50px;
          margin-left: 10px;
        }

        .morePushCart{
          float: right;
          width: 60px;
          height: 30px;
          line-height: 30px;
          text-align: center;
          margin-top: 10px;
          background-color: rgb(60,133,39);
          color: white;
          font-size: 12px;
          font-weight: bold;
          border-radius: 5px;
          margin-right: 10px;

          &:hover{
            cursor: pointer;
          }
        }
      }
    }
  }
}

.seckill{
  height: 260px;
  width: 1300px;
  margin: 20px auto;

  .seckill-left{
    display: inline-block;
    height: 260px;
    width: 190px;

    .seckill-left-text{
      height: 50px;
      width: 190px;
      font-size: 30px;
      color: white;
      font-weight: bold;
      text-align: center;
      padding-top: 40px;
      float: left;
    }

    .seckill-left-text1{
      height: 50px;
      width: 190px;
      font-size: 15px;
      color: white;
      text-align: center;
      padding-top: 120px;
      float: left;
    }

    .seckill-left-text2{
      height: 50px;
      width: 190px;
      font-size: 20px;
      color: white;
      font-weight: bold;
      text-align: center;
      padding-top: 30px;
      float: left;

      .seckill-left-text2-one{
        width: 30px;
        height: 30px;
        background-color: rgb(47,52,48);
        display: inline-block;
        padding-top: 4px;
      }
    }

    &:hover{
      cursor: pointer;
    }
  }

  .seckill-right{
    height: 260px;
    width: 1110px;
    display: inline-block;
    background-color: white;

    .seckill-right-item{
      display: inline-block;
      height: 260px;
      width: 200px;
      text-align: center;
      margin-left: 18px;

      .seckill-right-item-image{
        display: inline-block;
        width: 140px;
        height: 140px;
        float: left;
        margin-left: 30px;
        margin-top: 30px;
      }

      .seckill-right-item-text{
        width: 200px;
        height: 20px;
        float: left;
        margin-top: 10px;
        font-size: 12px;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        line-height: 23px;
        max-height: 20px;
        -webkit-line-clamp: 1;
        -webkit-box-orient: vertical;
      }

      .seckill-right-item-pay{
        width: 200px;
        height: 20px;
        float: left;
        color: #e1251b;
        font-size: 12px;
        font-weight: 700;
        margin-top: 15px;
      }

      &:hover{
        cursor: pointer;
        color: rgb(60,135,39);
      }
    }
    .seckill-right-more{
      padding-top: 20px;
      height: 260px;
      width: 1100px;
      display: inline-block;
      text-align: center;
      color: rgb(60,135,39);
      font-weight: bold;
      font-size: 20px;
      float: left;

      &:hover{
        background-color: rgb(117,117,117);
        cursor: pointer;
      }
    }
  }
}

.tag_menu{
  width: 1300px;
  margin: 0 auto;
}
.tag {
  height: 60px;
  width: 1300px;
  background-color: rgb(60,135,39);
  text-align: center;
  //margin: 20px auto;
  //margin-left: 300px;
  margin-top: 20px;
  margin-bottom: 20px;

  .span-more {
    display: block;
    font-size: 15px;
    float: right;
    margin-right: 20px;
    height: 40px;
    width: 80px;

    &:hover {
      cursor: pointer;
      border-bottom: 2px solid white;
    }
  }
}
.tag span {
  line-height: 60px;
  color: white;
  font-size: 20px;
  font-weight: bold;
  float: left;
  margin-left: 20px;
}

.store {
  // background: url("../../assets/storeBox/storeBox.jpg");
  background-color: rgb(245,245,245);
}
.store-div {
  width: 1325px;
  height: 100%;
  //margin-left: 275px;

  .title-span {
    display: inline-block;
    margin-top: 35px;
    font-size: 13px;
    font-weight: bold;
    height: 46px;
    width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    line-height: 23px;
    max-height: 46px;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    margin-left: 15px;
  }
}
.store-list {
  width: 240px;
  height: 340px;
  margin-right: 25px;
  margin-bottom: 25px;
  text-align: center;
  border-top: 5px solid white;
  border-bottom: 5px solid white;
  border-left: 5px solid white;
  border-right: 5px solid white;
  background-color: white;

  .store-list-image {
    margin-top: 30px;
    width: 150px;
    height: 150px;
  }

  .title {
    display: inline-block;
    margin: 0 auto;
    //border-top: 4px solid #000000;
    //background-color: rgb(168,127,16);
    //border-top: 2px solid rgb(216,171,54);
    //border-left: 2px solid rgb(216,171,54);
    //border-bottom: 2px solid rgb(79,54,0);
    //border-right: 2px solid rgb(79,54,0);
    //border-radius: 3px;
    width: 200px;
    height: 25px;
  }

  .title-div {
    height: 20px;
    text-align: left;

    span {
      display: inline-block;
      font-size: 20px;
      font-weight: bold;
      color: red;
    }

    .title-money {
      width: 25px;
      height: 25px;
      vertical-align: middle;
      // margin-left: 20px;
    }
  }

  //.title-border {
    //display: inline-block;
    //width: 206px;
    //height: 46px;
    //margin: 0px auto;
    //background-color: black;
    //border-radius: 6px;

    //&:hover {
      //.title {
      //  cursor: pointer;
      //  display: inline-block;
      //  //border-top: 4px solid #000000;
      //  margin: 3px auto;
      //  background-color: rgb(198,198,198);
      //  border-top: 2px solid rgb(248,250,249);
      //  border-left: 2px solid rgb(248,250,249);
      //  border-bottom: 2px solid rgb(87,87,87);
      //  border-right: 2px solid rgb(87,87,87);
      //  width: 200px;
      //  height: 40px;
      //  border-radius: 3px;
      //}
    //}
  //}

  .button {
    display: inline-block;
    position: relative;
    bottom: 100px;
    width: 160px;
    height: 40px;
    margin: 0 auto;
    background-color: rgb(14,133,67);
    border-top: 3px solid rgb(95,171,133);
    border-bottom: 3px solid rgb(0,90,42);
    border-left: 3px solid rgb(95,171,133);
    border-right: 3px solid rgb(0,90,42);
    text-align: center;
    color: white;
    font-weight: bolder;
    font-size: 13px;
    opacity:0;
  }

  &:hover {
    cursor: pointer;
    //width: 300px;
    //height: 360px;
    //margin-left: 80px;
    //margin-bottom: 50px;
    //text-align: center;
    //border-top: 5px solid rgb(128,176,114);
    //border-bottom: 5px solid rgb(128,176,114);
    //border-left: 5px solid rgb(45,100,29);
    //border-right: 5px solid rgb(45,100,29);
    //background-color: white;

    //.button {
    //  display: inline-block;
    //  position: relative;
    //  bottom: 100px;
    //  width: 160px;
    //  height: 40px;
    //  margin: 0 auto;
    //  background-color: rgb(14,133,67);
    //  border-top: 3px solid rgb(95,171,133);
    //  border-bottom: 3px solid rgb(0,90,42);
    //  border-left: 3px solid rgb(95,171,133);
    //  border-right: 3px solid rgb(0,90,42);
    //  text-align: center;
    //  color: white;
    //  font-weight: bolder;
    //  font-size: 13px;
    //  opacity:1;
    //}
    //.button:hover{
    //  cursor: pointer;
    //  background-color: rgb(0,90,42);
    //  border-top: 3px solid rgb(14,133,67);
    //  border-bottom: 3px solid rgb(95,171,133);
    //  border-left: 3px solid rgb(14,133,67);
    //  border-right: 3px solid rgb(95,171,133);
    //}
  }
}

</style>
